'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    let result = await ctx.service.user.login(username, password);
    ctx.body = result
  }
  async getInfo() {
    const {ctx} = this
    let token = ctx.query.token
    let { username } = this.app.jwt.verify(token, this.app.config.jwt.secret)
    let result = await ctx.service.user.getInfo(username)
    ctx.body = result
  }
  async getUserList() {
    try {
      const { ctx } = this
      let roles = ctx.query.roles
      let list = await ctx.service.user.getUserList(roles)
      ctx.body = {
        code: 200,
        list
      }
    } catch(e) {
      this.ctx.body = {
        code: 0,
        message: e.message
      }
    }
  }
}

module.exports = HomeController;
